from business.register_business import RegisterBusiness
from selenium import webdriver
import unittest
import os
import HTMLTestRunner
from log.user_log import UserLog




class FirstCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.log = UserLog()
        cls.logger = cls.log.get_log()

        print('all cases before')
        cls.file_name = 'E:/study/autostudy/image/test01.png'

    #
    # @classmethod
    # def tearDownClass(cls):
    #     print('all cass after


    def setUp(self):

        print('case前置条件')
        self.driver = webdriver.Chrome()
        self.driver.get('http://www.5itest.cn/register')
        self.logger.info("this is chrome")

        self.register = RegisterBusiness(self.driver)


    def tearDown(self):
        print('case后置条件')
        for method_name,error in self._outcome.errors:
            if error:
                case_name = self._testMethodName
                file_path = os.path.join('E:/study/autostudy/image/'+case_name+'.png')
                self.driver.save_screenshot(file_path)

        self.driver.close()

    @classmethod
    def tearDownClass(cls):
        cls.log.close_handle()




    def test_register_email_error(self):
        email_error = self.register.register_email_error('34@163.com','user1','111111',self.file_name)
        self.assertFalse(email_error,'case执行失败')



    def test_register_username_error(self):
        user_error = self.register.register_name_error('3434@qq.com','use','111111',self.file_name)
        self.assertFalse(user_error,'case执行失败')

    def test_register_password_error(self):
        password_error = self.register.register_password_error('3434@qq.com','user1','111',self.file_name)
        self.assertFalse(password_error,'case执行失败')

    def test_register_code_error(self):
        code_error = self.register.register_code_error('3434@qq.com','user1','111111',self.file_name)
        self.assertFalse(code_error,'case执行失败')

    def test_login_success(self):
        success = self.register.user_base('224138170@qq.com','user1','111111',self.file_name)
        self.assertTrue(success,'case执行失败')

#
# def main():
#     first = FirstCase()
#     first.test_register_email_error()
#     first.test_register_username_error()
#     first.test_register_password_error()
#     first.test_register_code_error()
#     first.test_login_success()

if __name__ == '__main__':

    file_path = 'E:/study/autostudy/report/first_case.html'
    f = open(file_path,'wb')
    # unittest.main
    suite = unittest.TestSuite()
    # suite.addTest(FirstCase('test_register_email_error'))
    # suite.addTest(FirstCase('test_register_username_error'))
    # suite.addTest(FirstCase('test_register_password_error'))
    suite.addTest(FirstCase('test_register_code_error'))
    # suite.addTest(FirstCase('test_login_success'))

    runner = HTMLTestRunner.HTMLTestRunner(stream=f,title='first report',description="第一次测试报告",verbosity=2)
    runner.run(suite)